﻿using AJWPFAdmin.Core.Logger;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Services.EF;
using Masuit.Tools;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AJWPFAdmin.Services.AppointAPI.GoodsInfoQuery
{
    public class ShangHaiGoodsInfoQuery : GoodsInfoQueryBase
    {
        private AJLog4NetLogger _logger;

        public ShangHaiGoodsInfoQuery(AJConfigService cfgSvc, DefaultAppointAPI.APIConfig apiConfig, AJLog4NetLogger logger, GoodsInfoQueryTemplate name) : base(cfgSvc, apiConfig, name)
        {
            _logger = logger;
        }

        public override void FillGoodsInfo(ref Car carInfo, WeighingDataCache cacheData)
        {
            carInfo.Code = cacheData.TicketCode;
            carInfo.Sender = cacheData.Sender;
            carInfo.Receiver = cacheData.Receiver;

            carInfo.SendOrReceiveFlag = cacheData.SendOrReceiveFlag;

            if (carInfo.SendOrReceiveFlag == "收货")
            {
                carInfo.InGoodsName = cacheData.GoodsName;
                carInfo.InGoodsVolume = cacheData.GoodsWeight;
                carInfo.InGoodsType = cacheData.GoodsType;
            }
            else
            {
                carInfo.OutGoodsName = cacheData.GoodsName;
                carInfo.OutGoodsVolume = cacheData.GoodsWeight;
                carInfo.OutGoodsType = cacheData.GoodsType;
            }
        }

        public override async Task<WeighingDataCache> QueryAsync(CarInfoParams carInfoParams)
        {
            var carInfo = carInfoParams.CarInfo;

            var carNo = carInfo.CarNo;

            return await _db.WeighingDataCaches
                .Where(p => p.CarPlateNumber == carNo && DateTime.Now < p.ExpireDate).OrderByDescending(p => p.CreateDate).AsNoTracking().FirstOrDefaultAsync();


        }
    }
}
